<?php

class PageController extends Controller
{

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
		 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to access 'index' and 'view' actions.
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
		Yii::log('Index', 'info', 'appl');
		$this->render('index');
		echo 'Index';
	}
	/**
	 * Displays a particular model.
	 */
	public function actionView($id)
	{
		$now=new CDbExpression("NOW()");
		$page=$this->loadModel();
		$user=User::model()->findbyPk(Yii::app()->user->id);
		$comment=$this->newComment($page);
		$tags=Yii::app()->db->createCommand()
    		->select('t.name as name, p.url as url' )
    		->from('{{pagetag}} pt')
    		->join('{{page}} p', 'p.id=pt.page and p.startdate <='.$now.' and p.enddate >'.$now)
    		->join('{{tag}} t', 't.id=pt.tag')
    		->where('pt.page='.$id)
    		->order('t.name')
    		->queryall();
    
	$this->sidebaritems['tagCloudExcludes'] = $id;

    $webpage = Yii::app()->db->createCommand()
        ->select('p.heading as heading, type, hascomment')
        ->from('{{page}} p')
        ->where('p.id='.$id.' and p.startdate <='.$now.' and p.enddate >'.$now)
        ->queryRow();
    
    $section = Yii::app()->db->createCommand()
        ->select('id, s.heading as heading, typus')
        ->from('{{section}} s')
        ->where('s.page='.$id.' and s.startdate <='.$now.' and s.enddate >'.$now)
        ->order('s.sortorder')
        ->queryall();

     Yii::app()->db->createCommand('Update tbl_pagetag set frequency = frequency + 1 where page='.$id)->query();
		$this->render('view', array(
			'model'=>$page,
			'comment'=>$comment,
                        'webpage'=>$webpage,
                        'section'=>$section,
			'contents'=>$contents,
			'user'=>$user,
			'tags'=>$tags,
			'taggedpages'=>$page->taggedPages(),
		));
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
			{
				//if(Yii::app()->user->isGuest)
					//$condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;
				//else
				//	$condition='';
				$this->_model=Page::model()->findByPk($_GET['id'], $condition);
			}
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model; //$this->_model;
	}

	/**
	 * Creates a new comment.
	 * This method attempts to create a new comment based on the user input.
	 * If the comment is successfully created, the browser will be redirected
	 * to show the created comment.
	 * @param Post the post that the new comment belongs to
	 * @return Comment the comment instance
		 */
	protected function newComment($model)
	{
		$comment=new Comment;
		$page = Page::model()->findByPk($model->id);
		if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
		{
			echo CActiveForm::validate($comment);
			Yii::app()->end();
		}
		if(isset($_POST['Comment']))
		{
			$comment->attributes=$_POST['Comment'];
			if($page->addComment($comment))
			{
				//if($comment->status==Comment::STATUS_PENDING)
				//	Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
				$this->refresh();
			}
		}
		return $comment;
	}

}
